package spring.superrun.record.mvc.service.impl;

import spring.superrun.record.mvc.entity.SuperrunRecordranking;
import spring.superrun.record.mvc.dao.SuperrunRecordrankingDao;
import spring.superrun.record.mvc.service.SuperrunRecordrankingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SuperrunRecordranking)表服务实现类
 *
 * @author makejava
 * @since 2021-06-18 09:19:55
 */
@Service("superrunRecordrankingService")
public class SuperrunRecordrankingServiceImpl implements SuperrunRecordrankingService {
    @Resource
    private SuperrunRecordrankingDao superrunRecordrankingDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SuperrunRecordranking queryById(String id) {
        return this.superrunRecordrankingDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SuperrunRecordranking> queryAllByLimit(int offset, int limit) {
        return this.superrunRecordrankingDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param superrunRecordranking 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunRecordranking insert(SuperrunRecordranking superrunRecordranking) {
        this.superrunRecordrankingDao.insert(superrunRecordranking);
        return superrunRecordranking;
    }

    /**
     * 修改数据
     *
     * @param superrunRecordranking 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunRecordranking update(SuperrunRecordranking superrunRecordranking) {
        this.superrunRecordrankingDao.update(superrunRecordranking);
        return this.queryById(superrunRecordranking.getId());
    }

    @Override
    public List<SuperrunRecordranking> queryAllNoCondition(String uid) {
        List<SuperrunRecordranking> result= this.superrunRecordrankingDao
                                                    .queryAllNoCondition(uid);
        result.sort((q1,q2)->q2.getAvgspeed().compareTo(q1.getAvgspeed()));
        return result;
    }

    @Override
    public boolean AddOrUpdate(SuperrunRecordranking rankingIsExist) {
        SuperrunRecordranking ranking = queryByUserId(rankingIsExist.getUserid());
        if (null==ranking){
            ranking=  insert(rankingIsExist);
        }else {
            ranking = update(rankingIsExist);
        }
        return null!=ranking;
    }

    @Override
    public SuperrunRecordranking queryByUserId(String userid) {
        return superrunRecordrankingDao.queryByUserId(userid);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.superrunRecordrankingDao.deleteById(id) > 0;
    }
}
